package com.zhao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhao.Result;
import com.zhao.entity.*;
import com.zhao.entity.To.CourseTo;
import com.zhao.entity.Vo.CourseVo;
import com.zhao.entity.Vo.TeacherQuery;
import com.zhao.exceptionHandel.GuliException;
import com.zhao.mapper.*;
import com.zhao.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author xiaozhao
 * @since 2021-07-26
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseMapper courseMapper;
    @Autowired
    private EduCourseDescriptionMapper courseDescriptionMapper;
    @Autowired
    private EduTeacherMapper teacherMapper;
    @Autowired
    private EduSubjectMapper subjectMapper;
    @Autowired
    private EduChapterMapper chapterMapper;
    @Autowired
    private EduVideoMapper videoMapper;

    @Override
    //添加课程信息
    public String addCourse(CourseVo courseVo) {
        //先添加课程的基本信息
        EduCourse course=new EduCourse();
        //copyProperties  把前面的值复制给后面的
        BeanUtils.copyProperties(courseVo,course);
//        course.setPrice(courseVo.getPrice());
//        course.setCover(courseVo.getCover());
//        course.setId(courseVo.getId());
//        course.setTeacherId(courseVo.getTeacherId());
//        course.setTitle(courseVo.getTitle());
//        course.setSubjectId(courseVo.getSubjectId());
//        course.setLessonNum(courseVo.getLessonNum());
        int insert = courseMapper.insert(course);
        if(insert == 0){
            throw new GuliException(20001,"添加课程数据失败");
        }
        //获取课程id
        String id = course.getId();
        //再添加课程的简介信息
        EduCourseDescription eduCourseDescription=new EduCourseDescription();
        eduCourseDescription.setDescription(courseVo.getDescription());
        //为了使课程表和详细表有联系，使课程表的id和详细表的id的相同
        eduCourseDescription.setId(id);
        courseDescriptionMapper.insert(eduCourseDescription);
        return id;
    }

    @Override
    public CourseVo findAllInfo(String id) {
        CourseVo courseVoc= courseMapper.findAll(id);
        return courseVoc;
    }

    @Override
    //修改课程信息
    public String updateCourse(CourseVo courseVo) {
        //先创建课程的类
        EduCourse course=new EduCourse();
        //copyProperties  把前面的值复制给后面的
        BeanUtils.copyProperties(courseVo,course);
        int insert = courseMapper.updateById(course);
        if(insert == 0){
            throw new GuliException(20001,"修改课程数据失败");
        }
        //获取课程id
        String id = course.getId();
        //再添加课程的简介信息
        EduCourseDescription eduCourseDescription=new EduCourseDescription();
        eduCourseDescription.setDescription(courseVo.getDescription());
        //为了使课程表和详细表有联系，使课程表的id和详细表的id的相同
        eduCourseDescription.setId(id);
        courseDescriptionMapper.updateById(eduCourseDescription);
        return id;
    }

    @Override
    //根据课程id查询所有信息
    public CourseTo getAll(String id) {
        //首先根据课程id查询课程相关的信息
        EduCourse course = courseMapper.selectById(id);
        //根据课程id查询讲师信息
        EduTeacher teacher=teacherMapper.findTeacherByCourseId(id);
        //根据课程id查询到课程简介
        EduCourseDescription eduCourseDescription = courseDescriptionMapper.selectById(id);
        //根据课程id得到课程分类信息
        CourseTo courseTo= subjectMapper.getInfoByCousrId(id);
        System.out.println("111111"+courseTo);
        if(course==null){
            throw new GuliException(400,"课程信息为空");
        }
        if(teacher==null){
            throw new GuliException(400,"教师信息为空");
        }
        if(eduCourseDescription==null){
            throw new GuliException(400,"描述信息为空");
        }
        courseTo.setCourseTitle(course.getTitle());
        courseTo.setCover(course.getCover());
        courseTo.setDescription(eduCourseDescription.getDescription());
        courseTo.setPrice(course.getPrice());
        courseTo.setLessonNum(course.getLessonNum());
        courseTo.setTeacherName(teacher.getName());
        return courseTo;
    }

    @Override
    public int publicCourseById(String id) {
        int i= courseMapper.publicCousrByCourseId(id);
        return i;
    }

    @Override
    public Page findAllCourseInfo(TeacherQuery teacherQuery, Integer pageSize, Integer currentPage) {
        Page<CourseTo>page=new Page<>(currentPage,pageSize);
        Page<CourseTo> courseInfo = courseMapper.getAllCourseInfo(page,teacherQuery);
        return courseInfo;
    }

//    @Override
//    public List<CourseTo> findAllCourseInfo(Page<CourseTo> page, TeacherQuery teacherQuery) {
//        return courseMapper.getAllCourseInfo(page,teacherQuery);
//    }

    @Override
    public void changeStatus(String id, String status) {
        EduCourse course = courseMapper.selectById(id);
        //先判断课程的状态
        if(status.equals("Normal")){
            course.setStatus(status);
            courseMapper.updateById(course);
        }else{
            course.setStatus(status);
            courseMapper.updateById(course);
        }

    }

    @Override
    public Integer delPublicCourseById(String id) {
        int sum=0;
        try {
            //先删除课程的基本信息
            int i = courseMapper.deleteById(id);
            //再删除课程描述的基本信息
            int i1 = courseDescriptionMapper.deleteById(id);
            //再删除章节的基本信息
            QueryWrapper<EduChapter> wrapper=new QueryWrapper();
            wrapper.eq("course_id",id);
            Integer i2= chapterMapper.delete(wrapper);
            //删除小节信息
            QueryWrapper<EduVideo> videoWrapper=new QueryWrapper();
            videoWrapper.eq("course_id",id);
            Integer i3=videoMapper.delete(videoWrapper);
            if(i!=0&&11!=0&&i2!=0){
                sum++;
                return sum;
            }
        }catch (Exception e){
            System.out.println("发生异常,进行手动回滚！");
            // 手动回滚事务
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
        }
        return sum;
    }

}
